/*
 * test_pointrank.cpp
 *
 *  Created on: May 5, 2014
 *      Author: killerg
 */
//
//#include "share/util.h"
//#include <vector>
//#include <algorithm>
//#include <assert.h>
//
//using namespace std;
//
//struct player_t {
//	int64_t id;
//	int medal;
//	int group;
//};
//
//struct member_t {
//	int64_t id;
//	int64_t points;
//	member_t(int64_t id_, int64_t points_):
//		id(id_), points(points_){}
//};
//
//struct group_t {
//	vector<member_t> members;
//};
//
//struct pointrank_t {
//	vector<group_t> groups;
//};
//
//static pointrank_t* pointrank_create() {
//	pointrank_t* pr = new pointrank_t;
//	return pr;
//}
//
//static void pointrank_delete(pointrank_t* pr) {
//	delete pr;
//}
//
//static void pointrank_init(pointrank_t* pr, const Json::Value &json) {
//	pr->groups.clear();
//	const Json::Value &jgroups = json["groups"];
//	for (int i=0; i<jgroups.size(); ++i) {
//		const Json::Value &jgroup = jgroups.get(i, Json::nullValue);
//		pr->groups.push_back(group_t());
//		for (int ii=0; ii<jgroup.size(); ++ii) {
//			const Json::Value &jmember = jgroup.get(ii, Json::nullValue);
//			pr->groups.back().members.emplace_back(member_t(jmember["id"].asInt64(), jmember["pts"].asInt()));
//		}
//	}
//}
//
//static void pointrank_save(pointrank_t* pr, Json::Value &json) {
//	Json::Value &jgroups = json["groups"];
//	Json::Value jgroup;
//	Json::Value jmember;
//	for (auto &group : pr->groups) {
//		jgroup.clear();
//		for (auto &member : group.members) {
//			jmember["id"] = Json::Int64(member.id);
//			jmember["pts"] = Json::Int64(member.points);
//
//			jgroup.append(jmember);
//		}
//
//		jgroups.append(jgroup);
//	}
//}
//
//static void pointrank_print(pointrank_t* pr) {
//	Json::Value tmp;
//	pointrank_save(pr, tmp);
//	pointrank_init(pr, tmp);
//
////	printf("groups:%d\n", pr->groups.size());
////	for (auto &itr : pr->groups) {
////		printf("\tgroup:%d\n", itr.members.size());
////	}
//
//}
//
//
//
//static pointrank_t* g_pr;
//
//static void handle_pointrankcombat(player_t* player) {
//	int cur_group = player->group;
//	if (player->group < 0)
//		player->group = player->medal;
//
//	while (g_pr->groups.size() <= player->group)
//		g_pr->groups.push_back(group_t());
//
//	auto &group = g_pr->groups[player->group];
//	if (cur_group < 0)
//		group.members.push_back(member_t(player->id, 0));
//
//	int32_t idx = -1;
//	for (int i=0; i<group.members.size() && idx<0; ++i)
//		if (group.members[i].id == player->id)
//			idx = i;
//	if (idx < 0)
//		return;
//
//	int32_t other_idx = cut<int32_t>(idx+rand()%6-3, 0, group.members.size()-1);
//	assert(other_idx >= 0 && other_idx < group.members.size());
//
//	// combat
//	if (rand() % 2) {
//		group.members[idx].points += 100;
//		group.members[other_idx].points += 10;
//	} else {
//		group.members[idx].points += 10;
//		group.members[other_idx].points += 100;
//	}
//	// sort
//	static int counter = 0;
//	if (counter++ % 100 == 0) {
//		std::sort(
//				group.members.begin(),
//				group.members.end(),
//				[](const member_t &lhs, const member_t &rhs){
//					return lhs.points > rhs.points;
//				});
//
//	}
//}

#include <time.h>

static time_t _get_finish(time_t tt) {
	tm start = *localtime(&tt);

	if ((start.tm_wday == 3 && start.tm_hour < 12) || start.tm_wday < 3) {
		start.tm_hour = 12;
		start.tm_mday += 3-start.tm_wday;
	} else {
		start.tm_hour = 0;
		start.tm_mday += 7-start.tm_wday;
	}


	start.tm_min = 0;
	start.tm_sec = 0;

	return mktime(&start);
}

#include <vector>
#include <string>
using namespace std;

bool test_pointrank() {
	vector<time_t> times;


	time_t ttime = time(NULL);
	for (int i=0; i<7*24; ++i) {
		times.push_back(ttime + i * 60 * 60);
	}

	for (int i=0; i<times.size(); ++i) {
		string now = ctime(&times[i]);
		now[now.length()-1] = ' ';
		time_t nfin = _get_finish(times[i]);
		string finish = ctime(&nfin);
		finish[finish.length()-1] = ' ';
		printf("%d %s %s\n", times[i], now.c_str(), finish.c_str());
	}


//	vector<player_t*> players;
//	for (int i=0; i<10000; ++i) {
//		auto player = new player_t;
//		player->id = 1000000000 + i;
//		player->medal = rand() % 6;
//		player->group = -1;
//		players.push_back(player);
//	}
//
//	g_pr = pointrank_create();
//
//	for (int i=0; i<10000; ++i) {
//		handle_pointrankcombat(players[rand() % players.size()]);
//		if (i%10 == 0) {
//			pointrank_print(g_pr);
//		}
//	}
//
//	Json::Value save;
//	pointrank_save(g_pr,save);
//
//	pointrank_delete(g_pr);
//	g_pr = NULL;
//
//	for (auto &itr : players) {
//		delete itr;
//	}
//
//	save_file("pointsrank", to_cstr(save));

	return true;
}
